export default {
  // 计算总价格
  countTotal (state) {
    // 选中选中的商品，计算总价=商品总价格*商品总件数
    let total = 0
    const checkedItems = state.carts.items.filter(ele => ele.isChecked)
    checkedItems.forEach(ele => {
      total += ele.minPrice * ele.num
    })
    state.carts.total = parseFloat(total.toFixed(2))
  },
  // 改变state数据本身，比如数字，选中状态
  updateItem (state, items) {
    state.carts.items = items
  },
  // 单选改变全选状态
  allCheckedChange (state, allCheck) {
    state.carts.isAllChecked = allCheck
  },
  // 全选改变单选
  singleChekChange (state, checked) {
    state.carts.items.forEach(ele => {
      ele.isChecked = checked
    })
  },
  // 删除商品
  delItems (state, index) {
    state.carts.items.splice(index, 1)
  },
  // 加入购物车
  addCart (state, item) {
    state.carts.items.unshift(item)
  }
}
